﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.PublicShow
{
    public partial class SelectRoleGroup : BasePage
    {
        private Sys_RoleBLL srb = new Sys_RoleBLL();
        private Sys_RoleGroupBLL srgb = new Sys_RoleGroupBLL();

        private string Model
        {
            get
            {
                return FORM_MODE.Value;
            }
            set
            {
                FORM_MODE.Value = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Model = Request["MODE"] == "" ? "MU" : Request["MODE"];
                //if (!string.IsNullOrEmpty(Model))
                //{
                //    string nems = Model;
                //string SelectType = (Model == "1" ? "单选" : "多选");
                //}

                TreeNode t = new TreeNode("角色组汇总");
                MyTree.Nodes.Add(t);

                string name = "";
                string cid = "";
                DataTable dt = srgb.GetAllInfo();

                foreach (DataRow dr in dt.Rows)
                {
                    name = dr["rgro_name"].ToString();
                    cid = dr["rgro_id"].ToString();
                    string unit = "【" + "角色组" + "】";
                    TreeNode pNode = new TreeNode(name + unit, cid);
                    t.ChildNodes.Add(pNode);

                    LoadComNode(Convert.ToInt32(cid), pNode);
                    MyTree.ExpandDepth = 0;
                }
            }
        }

        //获取角色节点的递归方法
        private void LoadComNode(int parent, TreeNode pNode)
        {
            DataTable dataDep = srgb.GetAllInfo1(parent);
            if (dataDep.Rows.Count > 0)
            {
                for (int i = 0; i < dataDep.Rows.Count; i++)
                {
                    string unit = "【" + "角色" + "】";

                    TreeNode node = new TreeNode(dataDep.Rows[i]["role_name"].ToString() + unit, dataDep.Rows[i]["role_id"].ToString());
                    pNode.ChildNodes.Add(node);
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc = MyTree.CheckedNodes;
            try
            {
                int count = 0;
                int soncount = 0;
                string Ids = "";
                string Name = "";
                string SelectType = (Model == "1" ? "单选" : "多选");
                foreach (TreeNode item in tnc)
                {
                    if (item.Checked)
                    {
                        bool na0 = item.Text.Contains("【角色组】");
                        bool na1 = item.Text.Contains("【角色】");

                        bool na2 = false;
                        if (item.ChildNodes.Count > 0)
                        {
                            na2 = true;
                        }

                        if (na1 == false && na2 == true)
                        {
                            count += 1;
                            Name += item.Text.Substring(0, item.Text.IndexOf("【")) + ",";
                            Ids += item.Value + ",";
                        }
                        if (na0 == false && na1 == true)
                        {
                            soncount += 1;
                            Name += item.Text.Substring(0, item.Text.IndexOf("【")) + ",";
                            Ids += item.Value + ",";
                        }
                    }
                }

                string roleName = "";
                string roleGroupName = "";
                if (soncount <= 0 && count <= 0)
                {
                    MessageBox.Show(Page, "请至少选择一个角色");
                    return;
                }
                //else if (count > 0)
                //{
                //    roleGroupName = srgb.GetNamebyRoleGroupId(Ids.Substring(0, Ids.Length));
                //}
                else if (soncount > 0)
                {
                    roleName = srgb.GetNamebyRoleId(Ids.Substring(0, Ids.Length), Name);
                }

                MessageBox.ResponseScript(this, "FORM_SUBMIT(\"" + roleGroupName + roleName + "\");");
            }
            catch (Exception exp)
            {
                MessageBox.Show(Page, exp.Message.ToString());
                return;
            }
        }
    }
}